package com.cloud.school.bus.teacherhelper.base.view;

import com.cloud.school.bus.teacherhelper.R;

import android.R.integer;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * 通用的标题
 * 
 * @author lenovo
 * 
 */
public class TitleNavBarView extends LinearLayout implements OnClickListener, OnCheckedChangeListener {

	private Context mContext;

	private TextView mMessageTextView;
	private Button mLeftButton;
	private Button mRightButton;
	private RadioButton m2LeftRadioButton;
	private RadioButton m2RightRadioButton;

	private ViewGroup mMessageLayout;
	private ViewGroup mLeftLayout;
	private ViewGroup mRightLayout;
	private ViewGroup mTab2Layout;
	private ViewGroup mTitleBackground;

	private OnClickListener mLeftOnClickListener;
	private OnClickListener mRightOnClickListener;
	private OnCheckedChangeListener m2LeftOnCheckedChangeListener;
	private OnCheckedChangeListener m2RightOnCheckedChangeListener;

	public TitleNavBarView(Context context) {
		super(context);
		init(context);
	}

	public TitleNavBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	private void init(Context context) {
		mContext = context;

		LayoutInflater.from(context).inflate(R.layout.view_title_nav_bar, this,
				true);

		mMessageTextView = (TextView) findViewById(R.id.messageTextView);
		mMessageLayout = (ViewGroup) findViewById(R.id.messageLayout);
		mTab2Layout = (ViewGroup)findViewById(R.id.tab2Layout);
		
		mLeftButton = (Button) findViewById(R.id.leftButton);
		mLeftButton.setOnClickListener(this);
		mLeftLayout = (ViewGroup) findViewById(R.id.leftLayout);

		mRightButton = (Button) findViewById(R.id.rightButton);
		mRightButton.setOnClickListener(this);
		mRightLayout = (ViewGroup) findViewById(R.id.rightLayout);
		
		m2LeftRadioButton = (RadioButton)findViewById(R.id.left2RadioButton);
		m2LeftRadioButton.setOnCheckedChangeListener(this);
		m2RightRadioButton = (RadioButton)findViewById(R.id.right2RadioButton);
		m2RightRadioButton.setOnCheckedChangeListener(this);
		
		mTitleBackground = (ViewGroup)findViewById(R.id.titleBackground);
	}
	
	/**
	 * 设置标题背景
	 * @param color
	 */
	public void setTitleBackground(int color) {
		mTitleBackground.setBackgroundColor(mContext.getResources().getColor(color));
	}

	/**
	 * 设置标题message
	 * 
	 * @param messageString
	 */
	public void setMessage(String messageString) {
		mMessageLayout.setVisibility(View.VISIBLE);
		mTab2Layout.setVisibility(View.GONE);
		mMessageTextView.setText(messageString);
	}
	
	/**
	 * 
	 * @param messageString
	 * @param color
	 */
	public void setMessage(String messageString, int color) {
		mMessageLayout.setVisibility(View.VISIBLE);
		mTab2Layout.setVisibility(View.GONE);
		mMessageTextView.setText(messageString);
		mMessageTextView.setTextColor(color);
	}
	
	public void setMessageRightArrowOnClickListener(String messageString,int imageId, OnClickListener onClickListener) {
		Drawable img_off = mContext.getResources().getDrawable(imageId);
		img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
		mMessageTextView.setCompoundDrawables(null, null, img_off, null);
		mMessageTextView.setCompoundDrawablePadding(15);
		mMessageLayout.setVisibility(View.VISIBLE);
		mTab2Layout.setVisibility(View.GONE);
		mMessageTextView.setText(messageString);
		mMessageTextView.setOnClickListener(onClickListener);
	}

	/**
	 * 设置LeftButton的背景图片和显示文字
	 * 
	 * @param imageId
	 * @param string
	 */
	public void setLeftButtonImageString(int imageId, String string, int textColor) {
		mLeftButton.setBackgroundResource(imageId);
		mLeftButton.setTextColor(getResources().getColor(textColor));
		mLeftButton.setText(string);
	}
	
//	public void setLeftDrawableImage(int imageId) {
//		Drawable img_off = mContext.getResources().getDrawable(imageId);
//		img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
//		mLeftButton.setCompoundDrawables(img_off, null, null, null);
//		mLeftButton.setCompoundDrawablePadding(5);
//	}
	
	public void setRightBubble(String bubbleString) {
		ViewGroup bubbleLayout = (ViewGroup)findViewById(R.id.bubbleLayout);
		if("0".equals(bubbleString)){
			if(bubbleLayout.getVisibility() != View.GONE){
				bubbleLayout.setVisibility(View.GONE);
			}
		}else {
			if(bubbleLayout.getVisibility() != View.VISIBLE){
				bubbleLayout.setVisibility(View.VISIBLE);
			}
		}
		
		TextView bubbleTextView = (TextView)findViewById(R.id.bubbleTextView);
		bubbleTextView.setText(bubbleString);
	}

	/**
	 * 设置RightButton的背景图片和显示文字
	 * 
	 * @param imageId
	 * @param string
	 */
	public void setRightButtonImageString(int imageId, String string, int textColor) {
		mRightButton.setBackgroundResource(imageId);
		mRightButton.setTextColor(getResources().getColor(textColor));
		mRightButton.setText(string);
		ImageView rightImageView = (ImageView)findViewById(R.id.rightImageView);
		rightImageView.setVisibility(View.GONE);
		mRightButton.setVisibility(View.VISIBLE);
	}
	
	public void setRightImageView(int imageId, OnClickListener onClickListener){
		ImageView rightImageView = (ImageView)findViewById(R.id.rightImageView);
		rightImageView.setVisibility(View.VISIBLE);
		rightImageView.setImageResource(imageId);
		mRightButton.setVisibility(View.GONE);
		rightImageView.setOnClickListener(onClickListener);
	}
	
	/**
	 * 设置标签tab为2个左边的文字
	 * @param textString
	 */
	public void set2LeftRadioString(String textString){
		if(View.VISIBLE != mTab2Layout.getVisibility()){
			mTab2Layout.setVisibility(View.VISIBLE);
		}
		m2LeftRadioButton.setText(textString);
	}
	
	/**
	 * 设置标签tab为2个右边的文字
	 * @param textString
	 */
	public void set2RightRadioString(String textString){
		if(View.VISIBLE != mTab2Layout.getVisibility()){
			mTab2Layout.setVisibility(View.VISIBLE);
		}
		m2RightRadioButton.setText(textString);
	}

	public void set2LeftRadioChecked(boolean checked) {
		m2LeftRadioButton.setChecked(checked);
	}
	
	public void set2RightRadioChecked(boolean checked) {
		m2RightRadioButton.setChecked(checked);
	}
	
	/**
	 * 设置LeftButton是否可见
	 * 
	 * @param visibility
	 */
	public void setLeftButtonVisibility(int visibility) {
		mLeftLayout.setVisibility(visibility);
	}

	/**
	 * 设置RightButton是否可见
	 * 
	 * @param visibility
	 */
	public void setRightButtonVisibility(int visibility) {
		mRightLayout.setVisibility(visibility);
	}

	public void setLeftButtonOnClickListener(OnClickListener onClickListener) {
		mLeftOnClickListener = onClickListener;
	}

	public void setRightButtonOnClickListener(OnClickListener onClickListener) {
		mRightOnClickListener = onClickListener;
	}
	
	public void set2LeftRadioOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
		m2LeftOnCheckedChangeListener = onCheckedChangeListener;
	}

	public void set2RightRadioOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
		m2RightOnCheckedChangeListener = onCheckedChangeListener;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftButton: {
			if (null != mLeftOnClickListener) {
				mLeftOnClickListener.onClick(v);
			}
			break;
		}
		case R.id.rightButton: {
			if (null != mRightOnClickListener) {
				mRightOnClickListener.onClick(v);
			}
			break;
		}
		default:
			break;
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.left2RadioButton:{
			if(null != m2LeftOnCheckedChangeListener){
				m2LeftOnCheckedChangeListener.onCheckedChanged(buttonView, isChecked);
			}
			break;
		}
		case R.id.right2RadioButton:{
			if(null != m2RightOnCheckedChangeListener){
				m2RightOnCheckedChangeListener.onCheckedChanged(buttonView, isChecked);
			}
			break;
		}
		default:
			break;
		}
	}
}
